﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Windows.Media.Imaging;
using System.Text;
using Microsoft.Phone.Tasks;

namespace WindowsPhoneApplication1
{
    public partial class SingleStatusPage : PhoneApplicationPage
    {
        IDictionary<string, string> parameters;
        public SingleStatusPage()
        {
            InitializeComponent();
            Connectors.Connector.Ssp = this;
        }

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            parameters = this.NavigationContext.QueryString;
            base.OnNavigatedTo(e);
        }

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            long outLong;
            if (long.TryParse(parameters["SID"], out outLong))
                Connectors.StatusesConnector.GetSingleStatus(outLong);
            else if (NavigationService.CanGoBack)
                NavigationService.GoBack();
        }

        public void UpdateStatus(WebServiceDataSources.Status s)
        {
            Dispatcher.BeginInvoke(() =>
            {
                PageTitle.Text = s.user_path.Split('/').Last();

                //MessageBlock.Text = s.body;

                //if (s.body.Contains("http://"))
                //{

                    var paragraph = new Paragraph();

                    var runs = new List<Inline>();

                    foreach (var word in s.body.Split(' '))
                    {
                        Uri uri;

                        if (Uri.TryCreate(word, UriKind.Absolute, out uri) && word.StartsWith("http://")) //||
                           //(word.StartsWith("www.") && Uri.TryCreate("http://" + word, UriKind.Absolute, out uri)))
                        {
                            var link = new Hyperlink();
                            link.Inlines.Add(new Run() { Text = word });
                            link.Click += (sender, e) =>
                            {
                                var hyperLink = (sender as Hyperlink);
                                new WebBrowserTask() { Uri = uri }.Show();
                            };

                            runs.Add(link);
                        }
                        else
                        {
                            runs.Add(new Run() { Text = word });
                        }

                        runs.Add(new Run() { Text = " " });
                    }

                    foreach (var run in runs)
                        paragraph.Inlines.Add(run);

                        MessageBlock.Blocks.Add(paragraph);

                    //    MessagesListBox.Children.Add(textBlock);
                    //    MessagesListBox.UpdateLayout();
                //}


                if (s.pictures != null)
                {
                    foreach (WebServiceDataSources.Picture p in s.pictures)
                    {
                        string inMsgUrl = "";
                        var splitted = p.url.Split('.');
                        if (splitted.Length == 2)
                            inMsgUrl = splitted[0] + "_standard." + splitted[1];
                        else if (splitted.Length > 2)
                        {
                            StringBuilder toRet = new StringBuilder();

                            for (int i = 0; i < splitted.Length - 1; i++)
                            {
                                if (i != 0)
                                    toRet.Append(".");
                                toRet.Append(splitted[i]);
                            }
                            toRet.Append("_inmsg.jpg");

                            inMsgUrl = toRet.ToString();
                        }
                        BitmapImage bi = new BitmapImage(new Uri(inMsgUrl, UriKind.Absolute));
                        Image im = new Image();
                        im.Source = bi;
                        im.Width = 380;
                        im.Height = 380;

                        im.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;

                        im.Margin = new Thickness(8, 20, 8, 0);

                        MainPanel.Children.Add(im);
                    }
                }
            });
        }

        public void GoBack()
        {
            Dispatcher.BeginInvoke(() =>
            {
                if (NavigationService.CanGoBack)
                    NavigationService.GoBack();
            });
        }
    }
}
